<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>校园图书馆</title>
  
  <!-- 基础路径配置 -->
  <base href="/">
  
  <!-- 图标配置 -->
  <!-- iOS 设备图标 -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="校园图书馆">
  <link rel="apple-touch-icon" href="icons/apple-touch-icon.png">
  
  <!-- 标准 favicon -->
  <link rel="icon" type="image/x-icon" href="icons/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="icons/favicon-16x16.png">
  <link rel="icon" type="image/png" sizes="32x32" href="icons/favicon-32x32.png">
  
  <!-- Android 设备图标 -->
  <link rel="icon" type="image/png" sizes="192x192" href="icons/android-chrome-192x192.png">
  <link rel="icon" type="image/png" sizes="512x512" href="icons/android-chrome-512x512.png">
  
  <!-- Web App 清单 -->
  <link rel="manifest" href="icons/site.webmanifest">
  
  <!-- 主题色设置 -->
  <meta name="theme-color" content="#4285F4">
  
  <script>
    // 服务工作者版本号
    var serviceWorkerVersion = '1.0';
    
    // 加载主Dart文件
    function loadMainDartJs() {
      var scriptTag = document.createElement('script');
      scriptTag.src = 'main.dart.js';
      scriptTag.type = 'application/javascript';
      document.body.append(scriptTag);
    }
    
    // 初始化Flutter应用
    window.addEventListener('load', function(ev) {
      // 确保Flutter加载器可用
      if (typeof _flutter !== 'undefined' && _flutter.loader) {
        _flutter.loader.loadEntrypoint({
          serviceWorker: {
            serviceWorkerVersion: serviceWorkerVersion,
            serviceWorkerUrl: 'flutter_service_worker.js'
          },
          onEntrypointLoaded: async function(engineInitializer) {
            try {
              let appRunner = await engineInitializer.initializeEngine();
              await appRunner.runApp();
            } catch (error) {
              console.error('Flutter应用初始化失败:', error);
              showError(error);
            }
          }
        }).catch(function(error) {
          console.error('Flutter加载器初始化失败:', error);
          showError(error);
          // 回退到直接加载
          loadMainDartJs();
        });
      } else {
        console.error('Flutter加载器未定义，尝试直接加载');
        loadMainDartJs();
      }
    });
    
    // 显示错误信息
    function showError(error) {
      document.getElementById('loading').innerHTML = `
        <div style="padding: 20px; text-align: center;">
          <h2>应用初始化失败</h2>
          <p>${error.message || '未知错误'}</p>
          <p>请刷新页面重试或联系管理员</p>
          <button onclick="window.location.reload()" style="padding: 10px 20px; margin-top: 20px; background: #4285F4; color: white; border: none; border-radius: 4px; cursor: pointer;">
            刷新页面
          </button>
        </div>
      `;
    }
  </script>
</head>
<body>
  <!-- 加载状态显示 -->
  <div id="loading" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; background: white; z-index: 9999;">
    <div style="text-align: center;">
      <h2>校园图书馆</h2>
      <p>正在加载应用...</p>
      <div id="progress" style="width: 300px; height: 5px; background: #e0e0e0; margin: 20px auto; overflow: hidden;">
        <div id="progress-bar" style="height: 100%; width: 0%; background: #4285F4; transition: width 0.3s;"></div>
      </div>
      <p id="status">初始化中...</p>
    </div>
  </div>
  
  <!-- Flutter应用容器 -->
  <div id="flutter-app"></div>
  
  <script>
    // 更新加载状态
    function updateStatus(message, percent) {
      const statusEl = document.getElementById('status');
      const progressBar = document.getElementById('progress-bar');
      
      if (statusEl) statusEl.textContent = message;
      if (progressBar) progressBar.style.width = percent + '%';
    }
    
    // 初始状态
    updateStatus('正在启动应用...', 10);
    
    // 模拟加载进度
    let progress = 10;
    const interval = setInterval(() => {
      progress += Math.random() * 5;
      updateStatus('加载资源...', progress);
      
      if (progress >= 90) {
        clearInterval(interval);
        updateStatus('准备启动...', 90);
      }
    }, 300);
    
    // 应用加载完成后隐藏加载界面
    window.addEventListener('flutter-first-frame', function() {
      clearInterval(interval);
      updateStatus('启动完成', 100);
      
      setTimeout(() => {
        const loadingEl = document.getElementById('loading');
        if (loadingEl) {
          loadingEl.style.opacity = '0';
          setTimeout(() => {
            loadingEl.style.display = 'none';
          }, 500);
        }
      }, 1000);
    });
  </script>
</body>
</html>